package com.baitengsoft.patentservicemonitor.job.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baitengsoft.patentservicemonitor.job.cloud.patent.hub.enums.LegalStatusEnum;
import com.baitengsoft.patentservicemonitor.job.cloud.patent.hub.enums.MainLegalStatusEnum;
import com.baitengsoft.patentservicemonitor.job.model.hub.Patent;
import com.baitengsoft.patentservicemonitor.job.model.hub.PatentChangeInfo;
import com.baitengsoft.patentservicemonitor.job.model.hub.Transaction;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangze
 * @date 2018-08-30 8:49.
 **/
public class PatentHubTransferUtil {


    /**
     * 获取地址变更信息
     *
     * @param patent
     * @return
     */
    public static List<String> getAddressChangeInfo(Patent patent) {
        if (patent == null) {
            return null;
        }
        List<Transaction> transactions = patent.getTransactions();
        if (!(transactions != null && transactions.size() > 0)) {
            return null;
        }

        Transaction filterTransaction = transactions.stream().filter(x -> x.getType().indexOf(LegalStatusEnum.AddressChange.getName()) != -1).findFirst().orElse(null);
        if (filterTransaction == null) {
            return null;
        }
        List<String> list = new ArrayList<>();
        list.add(filterTransaction.getDate());
        list.add(filterTransaction.getContent());
        return list;
    }


    /**
     * 获取授权日
     *
     * @param patent
     * @return
     */
    public static String getApd(Patent patent) {
        return getFilterDate(patent, LegalStatusEnum.Authorization);

    }

    /**
     * 获取公开日
     *
     * @param patent
     * @return
     */
    public static String getPd(Patent patent) {
        if (patent == null) {
            return null;
        }
        List<Transaction> transactions = patent.getTransactions();
        if (!(transactions != null && transactions.size() > 0)) {
            return null;
        }
        Transaction filterTransaction = transactions.stream()
                .filter(x -> x.getType().indexOf("公开") != -1 || x.getType().indexOf("公布") != -1)
                .findFirst()
                .orElse(null);
        if (filterTransaction == null) {
            return null;
        }
        return filterTransaction.getDate();

    }

    /**
     * 获取实质审查日期
     *
     * @param patent
     * @return
     */
    public static String getSed(Patent patent) {
        return getFilterDate(patent, LegalStatusEnum.Exam);
    }

    private static String getFilterDate(Patent patent, LegalStatusEnum statusEnum) {
        if (patent == null) {
            return null;
        }
        List<Transaction> transactions = patent.getTransactions();
        if (!(transactions != null && transactions.size() > 0)) {
            return null;
        }

        Transaction filterTransaction = transactions.stream().filter(x -> x.getType().indexOf(statusEnum.getName()) != -1).findFirst().orElse(null);
        if (filterTransaction == null) {
            return null;
        }
        return filterTransaction.getDate();
    }


    /**
     * 获取失效时间和失效原因
     *
     * @param patent
     * @return 第一个是失效时间，第二个是失效原因
     */
    public static List<String> getInValid(Patent patent) {
        if (patent == null) {
            return null;
        }
        List<Transaction> transactions = patent.getTransactions();
        if (!(transactions != null && transactions.size() > 0)) {
            return null;
        }
        // 失效专利
        if (patent.getLegalStatus().equals(MainLegalStatusEnum.InValid.getName()) || patent.getLegalStatus().equals(MainLegalStatusEnum.Lose.getName())) {
            Transaction filterTransaction = patent.getTransactions().get(0);
            List<String> result = new ArrayList<>();
            result.add(filterTransaction.getDate());
            result.add(filterTransaction.getType());
            return result;
        } else {
            return null;
        }
    }


    public static PatentChangeInfo getPatentChangeInfo(Patent patent) {
        String dateFormat = "yyyy-MM-dd";
        String zlqzyStr = "专利权的转移";
        String zlsqqzyStr = "专利申请权的转移";
        if (patent == null) {
            return null;
        }

        if (patent.getTransactions() == null || patent.getTransactions().size() == 0) {
            return null;
        }

        Transaction filterTransation = patent.getTransactions().stream().filter(x -> x.getType().indexOf("转移") != -1)
                .sorted(Comparator.comparingLong(x -> DateUtil.parse(x.getDate(), dateFormat).getTime())).findFirst().orElse(null);
        if (filterTransation == null) {
            return null;
        }
        PatentChangeInfo patentChangeInfo = new PatentChangeInfo();
        patentChangeInfo.setTfd(filterTransation.getDate());
        if (zlqzyStr.equals(filterTransation.getType()) || zlsqqzyStr.equals(filterTransation.getType())) {
            String content = filterTransation.getContent();
            List<String> infoList = getChangeInfo(content);
            if (infoList != null && infoList.size() > 0) {
                patentChangeInfo.setPpa1(infoList.get(0));
                patentChangeInfo.setPaa(infoList.get(1));
                patentChangeInfo.setPa1(infoList.get(2));
                patentChangeInfo.setAa(infoList.get(3));

            }

        } else if (filterTransation.getType().indexOf("专利申请权") != -1 && filterTransation.getType().indexOf("专利权的转移") != -1) {
            System.out.println(patent.getId() + " 特殊的专利转移数据");
        }

        return patentChangeInfo;
    }

    private static List<String> getChangeInfo(String content) {
        String[] values = content.split("\\n");
        String blank = " ";
        int length = 4;
        if (values == null || values.length == 0) {
            return null;
        }
        // 转移前权利人
        String beforePeron = values[1].split("变更为")[0].split("由")[1];
        // 转移前地址
        String beforeAddress = values[2].split("变更为")[0].split("由")[1];

        String aferPerson = values[1].split("变更为")[1];

        String afterAddress = values[2].split("变更为")[1];

        if (beforeAddress.indexOf(blank) != -1) {
            beforeAddress = beforeAddress.split(blank)[1];
        }
        if (afterAddress.indexOf(blank) != -1) {
            afterAddress = afterAddress.split(blank)[1];
        }
        if (values.length >= length) {
            String otherAfterPerson = values[3].split("变更为")[1];
            if (otherAfterPerson.indexOf(blank) != -1) {
                otherAfterPerson = String.join(",", otherAfterPerson.split(blank));
                aferPerson += "," + otherAfterPerson;
            }

        }
        List<String> list = new ArrayList<>();
        list.add(beforePeron);
        list.add(beforeAddress.replace(";", StringUtils.EMPTY));
        list.add(aferPerson);
        list.add(afterAddress.replace(";", StringUtils.EMPTY));
        return list;

    }


}

